package com.example.administrator.printoutindemo;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.example.administrator.printoutindemo.base.BaseActivity;
import com.example.administrator.printoutindemo.services.Tools;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.List;

import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;


public class StoreOutStorageCheckList2Activity extends BaseActivity {

    private static final String WEBSERVICE_CONFIG = "TEXT";
    private static final int LOGINSUCCESS = 0;
    private static final int NETWORKERROR = 2;
    private static final int SYSERROR = 3;
    private static final int UPLOADSUCCESS = 4;
    private EditText s_cl_act2_shyj = null;
    private Button store_cl_act2_btn_yes = null;
    private Button store_cl_act2_btn_no = null;
    private ListView store_cl_act2_listView = null;
    private TextView tv_xiang_mu_ming_cheng = null;
    private TextView tv_shen_he_zhuang_tai = null;
    private TextView tv_xiang_mu_bian_hao = null;
    private StoreOutStorageCheckList2Adapter adapter = null;
    private OutStorageMaterialCheckList oli = null;
    private List<OutStorageMaterialCheckList> li_oli = null;
    private OutStorageList outStorageList = null;
    private String SYSException = "";
    @SuppressLint("HandlerLeak")
    private final Handler hanlder = new Handler() {
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case LOGINSUCCESS:

                    adapter.setList(li_oli);
                    store_cl_act2_listView.setAdapter(adapter);
                    tv_xiang_mu_ming_cheng.setText(li_oli.get(0).get_projectname());
                    tv_xiang_mu_bian_hao.setText(li_oli.get(0).get_projectname());
                    tv_shen_he_zhuang_tai.setText(li_oli.get(0).getCheck_status());
                    break;

                case NETWORKERROR:

                    Toast.makeText(StoreOutStorageCheckList2Activity.this,
                            "网络或系统异常!", Toast.LENGTH_LONG).show();
                    break;
                case SYSERROR:

                    Toast.makeText(StoreOutStorageCheckList2Activity.this,
                            "系统异常!" + SYSException, Toast.LENGTH_LONG).show();
                    break;
                case UPLOADSUCCESS:

                    Toast.makeText(StoreOutStorageCheckList2Activity.this, "审核成功！",
                            Toast.LENGTH_LONG).show();

                    finish();
//                    Intent in = new Intent();
//                    in.setClass(StoreOutStorageCheckList2Activity.this, StoreOutStorageCheckList1Activity.class);
//                    startActivity(in);

                    break;

                default:
                    break;
            }

        }
    };
    private SharedPreferences sPreferencces;
    private String user_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_activity_outstoragechecklist_2);
        ToolbarUtils.initToolbarTitleBack(this, "审核");
        sPreferencces = getSharedPreferences(WEBSERVICE_CONFIG, 0);
        store_cl_act2_btn_yes = (Button) findViewById(R.id.store_cl_act2_btn_yes);
        store_cl_act2_btn_no = (Button) findViewById(R.id.store_cl_act2_btn_no);
        tv_xiang_mu_ming_cheng = (TextView) findViewById(R.id.tv_xiang_mu_ming_cheng);
        tv_xiang_mu_bian_hao = (TextView) findViewById(R.id.tv_xiang_mu_bian_hao);
        tv_shen_he_zhuang_tai = (TextView) findViewById(R.id.tv_shen_he_zhuang_tai);
        store_cl_act2_listView = (ListView) findViewById(R.id.store_cl_act2_listView);
        View mFooter = LayoutInflater.from(this).inflate(R.layout.include_shen_he_yi_jian, null);  //加载footer的布局
        s_cl_act2_shyj = (EditText) mFooter.findViewById(R.id.s_cl_act2_shyj);
        store_cl_act2_listView.addFooterView(mFooter);
        adapter = new StoreOutStorageCheckList2Adapter(this);
        oli = new OutStorageMaterialCheckList();
        outStorageList = new OutStorageList();
//		final int user_id = ((dyjApplication) getApplication()).get_user_id();// 审核人ID
        user_id = sPreferencces.getString(BaseConstants.UID, "");//审核人ID

        store_cl_act2_btn_yes.setOnClickListener(new store_cl_act2_btn_yes_click());
        store_cl_act2_btn_no.setOnClickListener(new store_cl_act2_btn_no_click());

        if (Integer.parseInt(user_id) != 0) {
            LoadingDialogUtil.showGifdialog2(getSupportFragmentManager(), R.drawable.public_loading);
            String out_storagelist_batchid = getIntent().getStringExtra("outCheckProject");
            getCheckMaterial(out_storagelist_batchid);

        } else {
            Tools.showDialogInfo(this, "登录已过期，请重新登录");
        }

    }

    /**
     * 获取审核物料清单列表
     *
     * @param out_storagelist_batchid
     */
    private void getCheckMaterial(String out_storagelist_batchid) {

        String methodName = "GetCheckMaterial";

        SoapParams params = new SoapParams();
        params.put("out_storagelist_batchid", out_storagelist_batchid);

        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();
                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    if (li_oli != null) li_oli.clear();
                    li_oli = oli.getListFromJSON(result);
                    adapter.setList(li_oli);
                    store_cl_act2_listView.setAdapter(adapter);
                    tv_xiang_mu_ming_cheng.setText(li_oli.get(0).get_projectname());
                    tv_xiang_mu_bian_hao.setText(li_oli.get(0).get_projectname());
                    tv_shen_he_zhuang_tai.setText(li_oli.get(0).getCheck_status());

                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
                ArmsUtils.makeText(getContext(), error.getMessage());

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();

            }
        }, getAapiUserName(), getApiPwd());

    }

    /**
     * 审核通过
     *
     * @param dpid
     * @param reason
     * @param person_id
     */
    private void checkAgree(String dpid, String reason, String person_id) {

//        String methodName = "CheckAgree";
        String methodName = "CheckAgreeNew";  //public string CheckAgreeNew(string batchId, string reason, int person_id) 2020-05-19 新加的方法  dpid就是batchId

        SoapParams params = new SoapParams();
//        params.put("dpid", dpid);
        params.put("batchId", dpid);
        params.put("reason", reason);
        params.put("person_id", person_id);

        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();
                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    Toast.makeText(StoreOutStorageCheckList2Activity.this, "审核成功！", Toast.LENGTH_LONG).show();
                    finish();
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
                ArmsUtils.makeText(getContext(), error.getMessage());

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();

            }
        }, getAapiUserName(), getApiPwd());

    }

    /**
     * 审核不通过
     *
     * @param dpid
     * @param reason
     * @param person_id
     */
    private void checkDisagree(String dpid, String reason, String person_id) {

//        String methodName = "CheckDisagree";
        String methodName = "CheckDisAgreeNew";   //public string CheckDisAgreeNew(string batchId, string reason, int person_id) 2020-05-19 新加的方法  dpid 就是batchId

        SoapParams params = new SoapParams();
//        params.put("dpid", dpid);
        params.put("batchId", dpid);
        params.put("reason", reason);
        params.put("person_id", person_id);

        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();
                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    Toast.makeText(StoreOutStorageCheckList2Activity.this, "审核成功！", Toast.LENGTH_LONG).show();
                    finish();
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
                ArmsUtils.makeText(getContext(), error.getMessage());

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();

            }
        }, getAapiUserName(), getApiPwd());

    }


    // 审核通过
    private class store_cl_act2_btn_yes_click implements OnClickListener {
        @Override
        public void onClick(View v) {

            if (li_oli != null && li_oli.size() > 0) {

                LoadingDialogUtil.showGifdialog2(getSupportFragmentManager(), R.drawable.public_loading);
                if (li_oli != null && li_oli.size() > 0) {

                    String out_storagelist_batchid = li_oli.get(0).get_out_storagelist_batchid();
                    String shyj = s_cl_act2_shyj.getText().toString();// 审核意见
                    if (!TextUtils.isEmpty(out_storagelist_batchid) && !TextUtils.isEmpty(shyj) && !TextUtils.isEmpty(user_id))
                        checkAgree(out_storagelist_batchid, shyj, user_id);

                }
            }

        }
    }

    // 审核未通过
    private class store_cl_act2_btn_no_click implements OnClickListener {
        @Override
        public void onClick(View v) {
            if (li_oli != null && li_oli.size() > 0) {

                LoadingDialogUtil.showGifdialog2(getSupportFragmentManager(), R.drawable.public_loading);
                if (li_oli != null && li_oli.size() > 0) {

                    String out_storagelist_batchid = li_oli.get(0).get_out_storagelist_batchid();
                    String shyj = s_cl_act2_shyj.getText().toString();// 审核意见
                    if (!TextUtils.isEmpty(out_storagelist_batchid) && !TextUtils.isEmpty(shyj) && !TextUtils.isEmpty(user_id))
                        checkDisagree(out_storagelist_batchid, shyj, user_id);
                }

            }
        }
    }
}